package com.icinfo.cloud.provider.common.crontask;

import com.icinfo.cloud.provider.punish.config.service.IConfigPaymentSourceBaseinfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * @author lilihao
 * @date 2023/02/24
 */
@Component
@Slf4j
public class ConfigPaymentSourceBaseInfoTask {
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private IConfigPaymentSourceBaseinfoService configPaymentSourceBaseinfoService;

    private static final String SYN_PAYMENT_BASE_INFO_REDIS_KEY = "PUNISH:TASK:SYN_PAYMENT_BASE_INFO_REDIS_KEY_NEW";

    /**
     * 描述：同步支付基础信息
     * @param
     * @return
     * @author lilihao
     * @date 2023/2/24 15:32
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void synchronizePaymentBaseInfo() {
        //防止多台服务器重复执行（过期时间设置为10分钟，预防服务器时间不同步）
        if (!redisTemplate.opsForValue().setIfAbsent(SYN_PAYMENT_BASE_INFO_REDIS_KEY, SYN_PAYMENT_BASE_INFO_REDIS_KEY, 10, TimeUnit.MINUTES)) {
            log.info("--------当前服务器未拿到分布式锁，不执行逻辑----------");
            return;
        }
        LocalDateTime startTime = LocalDateTime.now();
        log.info("synchronizePaymentBaseInfo start-startTime:{}", startTime);
        configPaymentSourceBaseinfoService.syncPaymentBaseInfoListBatch("330000022");
        LocalDateTime endTime = LocalDateTime.now();
        Duration duration = Duration.between(startTime, endTime);
        long seconds = duration.getSeconds();
        log.info("synchronizePaymentElectricInvoice end-endTime:{},consume:{} s", endTime, seconds);
    }
}
